package bloomers

/**
* allows a user to log in to their account - and if three failed logins, the user is added to the locked list and blocked from their account
*/

class UserController {

    def index() {}
    
    static def loginAttempts = new Integer(0)
    
    /**
    * controls the user login; redirects them to the user home page if the login is successul - or adds the user to the locked list after three failed login attempts
    */
    
    def doLogin = {
      
        
        def user = User.findByEmailAndPassword(params.email, params.password)
        
        println(user.firstName+" "+user.lastName+" "+user.email+" "+user.password)
        
        def usersFile = new File( '/Users/hryals3/NetBeansProjects/Bloomers/grails-app/conf/Resources/bloomersUsers.txt' )
        
        def users = User.getAll()
        
        def emails = "user@user.com"
        
        def driveInfo = [:]
            // Step through each line in the file
            usersFile.eachLine { line ->
                // If the line isn't blank
                if( line.trim() ) {
                    // Split into a key and value
                    def userFields = line.split( '\t' ).collect { it.trim() }
                    emails = emails + " " + userFields[0]
                }
            }
            
        for(User use : users){
           if(!emails.contains(use.email))
           usersFile.append("\n" + use.email + "\t"
                        + use.password + "\t"
                        + use.role + "\t"
                        + use.firstName + "\t"
                        + use.lastName + "\t"
                        + use.gender + "\t"
                        + use.city + "\t"
                        + use.state + "\t"
                        + use.country + "\t"
                        + use.zipcode)
       }
        
        
    
    if(user){
        
            
        
        if(!user.lockedList.isEmpty()){
            for(int i = 0; i < user.lockedList.size(); i++){
               // print i
               // print user.lockedList.size()
               // print user.lockedList.get(i)
                //print user
                //print user.id.equals(user.lockedList.get(i).id)
               if(user.id.equals(user.lockedList.get(i).id)){
                  //print "hi"
                  flash.message = "Please contact an admin"
                  redirect(action:"index")
                  return
              }
            }
                //print user.locked
                session.user = user
                flash.message = "Hello ${user.email}!"
                redirect (controller:'userHome')
            
         } //end nested if
         
            
            
        else{
                print user.locked
                session.user = user
                flash.message = "Hello ${user.email}!"
                redirect (controller:'userHome')

            }
    } //end big if
        
    else{
        //print loginAttempts
        loginAttempts++
        //print loginAttempts
            if (loginAttempts >= 3){
                def u = User.findByEmail(params.email)
                u.lockedList.add(u)
                flash.message = "Sorry ${params.email}, your account has been locked, please contact an administrator"
                loginAttempts = 0
            }
        //flash.message = "Sorry, ${params.email}. Please try again."
        redirect (action:'index')
        }
    
    }
    
    
}
